import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import UserLayout from '@/layouts/UserLayout.vue'
import AdminLayout from '@/layouts/AdminLayout.vue'
import UserDashboard from '@/views/user/Dashboard.vue'
import EquipmentList from '@/views/user/EquipmentList.vue'
import MyRentals from '@/views/user/MyRentals.vue'
import Profile from '@/views/user/Profile.vue'
import AdminDashboard from '@/views/admin/Dashboard.vue'
import EquipmentManagement from '@/views/admin/EquipmentManagement.vue'
import RentalRecords from '@/views/admin/RentalRecords.vue'
import CustomerManagement from '@/views/admin/CustomerManagement.vue'
import SystemSettings from '@/views/admin/SystemSettings.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/user',
      component: UserLayout,
      meta: { requiresAuth: true, role: 'USER' },
      children: [
        {
          path: '',
          name: 'UserDashboard',
          component: UserDashboard
        },
        {
          path: 'equipment',
          name: 'EquipmentList',
          component: EquipmentList
        },
        {
          path: 'my-rentals',
          name: 'MyRentals',
          component: MyRentals
        },
        {
          path: 'profile',
          name: 'Profile',
          component: Profile
        }
      ]
    },
    {
      path: '/admin',
      component: AdminLayout,
      meta: { requiresAuth: true, role: 'ADMIN' },
      children: [
        {
          path: '',
          name: 'AdminDashboard',
          component: AdminDashboard
        },
        {
          path: 'equipment',
          name: 'EquipmentManagement',
          component: EquipmentManagement
        },
        {
          path: 'rentals',
          name: 'RentalRecords',
          component: RentalRecords
        },
        {
          path: 'customers',
          name: 'CustomerManagement',
          component: CustomerManagement
        },
        {
          path: 'settings',
          name: 'SystemSettings',
          component: SystemSettings
        }
      ]
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  if (to.path === '/login' || to.path === '/register') {
    if (authStore.isLoggedIn) {
      if (authStore.isAdmin()) {
        next('/admin')
      } else {
        next('/user')
      }
    } else {
      next()
    }
    return
  }
  
  if (to.meta.requiresAuth) {
    const savedUser = localStorage.getItem('user')
    if (!authStore.isLoggedIn && savedUser) {
      try {
        await authStore.getCurrentUser()
      } catch {
        authStore.user = null
        authStore.isLoggedIn = false
        localStorage.removeItem('user')
        next('/login')
        return
      }
    } else if (!authStore.isLoggedIn) {
      next('/login')
      return
    }
    
    if (to.meta.role === 'ADMIN' && !authStore.isAdmin()) {
      next('/user')
      return
    }
    
    if (to.meta.role === 'USER' && authStore.isAdmin()) {
      next('/admin')
      return
    }
  }
  
  next()
})

export default router